﻿
namespace UDS.Management.SMSv2
{
    using System.Runtime.InteropServices;

    internal static class NativeMethods
    {
        private const string SMSDll = "SMSDLL.dll";

        /// <summary>
        /// Start SMS service.
        /// </summary>
        /// <param name="nPort">Serial port number. e.g., 1 for COM1</param>
        /// <param name="baudRate">The value should be 115200.</param>
        /// <param name="parity">The value should be 2</param>
        /// <param name="dataBits">The value should be 8.</param>
        /// <param name="stopBits">The value should be 0.</param>
        /// <param name="flowControl">The value should be 0.</param>
        /// <param name="csca">The default value is "card". Or use the correct number like "+8613800591500"</param>
        /// <returns>1: success; 0: error</returns>
        [DllImport(SMSDll, CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
        internal static extern int SMSStartService(
            int nPort, int baudRate, int parity, int dataBits,
            int stopBits, int flowControl, string csca);

        /// <summary>
        /// Send SMS.
        /// </summary>
        /// <param name="msg">
        /// The content of the message. It can have up to 70 characters if the content is Chinese, 
        /// or 140 characters if the content is English.
        /// </param>
        /// <param name="phoneNo">Phone number. The value should look like: 13800591500</param>
        /// <returns>
        /// The 1-based index of the message.
        /// </returns>
        [DllImport(SMSDll, CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
        internal static extern uint SMSSendMessage(string msg, string phoneNo);

        /// <summary>
        /// Queries whether the specified message has been successfully sent out.
        /// </summary>
        /// <param name="index">The 1-based index returned by <see cref="SMSSendMessage"/>.</param>
        /// <returns>
        /// 0: failed; 1: succeeded; -1: the message may still be under processing.
        /// </returns>
        [DllImport(SMSDll, CallingConvention = CallingConvention.StdCall)]
        internal static extern int SMSQuery(uint index);

        /// <summary>
        /// Stop SMS service.
        /// </summary>
        [DllImport(SMSDll, CallingConvention = CallingConvention.StdCall)]
        internal static extern int SMSStopSerice();
    }
}
